/**
 * @projectName spring-boot-examples-master
 * @package com.leo.springboot.service.impl
 * @className com.leo.springboot.service.impl.TestServiceImpl
 * @copyright Copyright 2020 XiaoFeiPeng, Inc All rights reserved.
 */
package com.leo.springboot.service.impl;

import com.leo.springboot.service.TestService;
import com.leo.springboot.strategy.ActivityStrategyService;

import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * TestServiceImpl
 * @description
 * @author jiangyunfei
 * @date 2023/11/16 11:08
 * @version 1.0
 */

@Service
public class TestServiceImpl implements TestService {

    @Resource
    private ActivityStrategyService activityStrategyService;


    @Override
    public String test(Integer code){
        ActivityFunction<Integer> function = strategyMap.get(code);
        //这里防止活动编号没匹配上，可以使用断言来判断从而抛出统一异常
        return function.dealActivity();
    }


    @FunctionalInterface
    interface ActivityFunction<A>{
        String dealActivity();
    }

    private final Map<Integer, ActivityFunction> strategyMap = new HashMap<>();

    /**
     * 初始化策略
     */
    @PostConstruct
    public void initDispatcher(){
        strategyMap.put(1, activityStrategyService::dealSpringActivity);
        strategyMap.put(2, activityStrategyService::dealSummerActivity);
        strategyMap.put(3, activityStrategyService::dealAutumnActivity);
        strategyMap.put(4, activityStrategyService::dealWinterActivity);
    }


}
